<?php
namespace Activity\Model;
use Think\Model;
class ActivityReplyModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_activity_reply'; 
	
	public function getRelationReply($data){
		if(!empty($data['activityid'])){
			$map['qyx_activity_reply.activityid']  = $data['activityid'];
		}
		
		if(!empty($data['custid'])){
			$map['qyx_activity_reply.custid']  = $data['custid'];
		}
		
		if(!empty($data['replyids'])){
			$map['qyx_activity_reply.replyid']  = array('in' , $data['replyids']);
		}
		
		
		$fields = array('qyx_activity_reply.replyid','qyx_activity_reply.activityid','qyx_activity_reply.custid',
				'qyx_activity_reply.pcustid','qyx_activity_reply.content','qyx_activity_reply.type',
				'qyx_activity_reply.createtime',
				'qyx_activity.cover','qyx_activity.title');
		
		if(empty($data['sortField']))$data['sortField'] = "qyx_activity_reply.replyid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
		
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('join qyx_activity on qyx_activity_reply.activityid = qyx_activity.activityid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else 
			$result = $this->join('join qyx_activity on qyx_activity_reply.activityid = qyx_activity.activityid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
		
		return $result;
	}
	
	
	
	public function addReply($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function getReplyByActivityId($activityid){
		$map['activityid'] = $activityid;
		$result = $this->field('activityid,replyid,custid,pcustid,content,type,status,createtime')->where($map)->order("replyid desc")->select();
		return $result;
	}
	
	public function getReplyById($replyid){
		$map['replyid'] = $replyid;
		$result = $this->field('activityid,replyid,custid,pcustid,content,type,status,createtime')->where($map)->find();
		return $result;
	}
	
	public function deleteReply($data){
		$map['replyid'] = $data['replyid'];
		$map['custid'] = $data['custid'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
	public function addPraise($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$map['type'] = $data['type'];
		$ret = $this->where($map)->find();
		if(empty($ret))
			$replyid = $this->add($data);
		else 
			$replyid = $ret['replyid'];
		return $replyid;
	}
	
	public function deletePraise($data){
		$map['activityid'] = $data['activityid'];
		$map['custid'] = $data['custid'];
		$map['type'] = $data['type'];
		$result = $this->where($map)->delete();
		return $result;
	}
	
}


